package cn.iocoder.yudao.module.picturebook.controller.admin.classes;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.module.picturebook.enums.ErrorCodeConstants.CLASSES_CREATE_FAIL;

import cn.iocoder.yudao.module.picturebook.controller.admin.classes.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.classes.ClassesDO;
import cn.iocoder.yudao.module.picturebook.service.classes.ClassesService;

@Tag(name = "绘本系统 - 班级信息")
@RestController
@RequestMapping("/picturebook/classes")
@Validated
public class ClassesController {

    @Resource
    private ClassesService classesService;

    @PostMapping("/create")
    @Operation(summary = "创建班级信息")
    @PreAuthorize("@ss.hasPermission('picturebook:classes:create')")
    public CommonResult<Long> createClasses(@Valid @RequestBody ClassesSaveReqVO createReqVO, @RequestHeader("token") Integer userId) {
        createReqVO.setTeacherId(Long.valueOf(userId));
        Long classes = classesService.createClasses(createReqVO);
        if (classes == null) {
            return CommonResult.error(CLASSES_CREATE_FAIL);
        }
        return success(classes);

    }

    @PutMapping("/update")
    @Operation(summary = "更新班级信息")
    @PreAuthorize("@ss.hasPermission('picturebook:classes:update')")
    public CommonResult<Boolean> updateClasses(@Valid @RequestBody ClassesSaveReqVO updateReqVO) {
        classesService.updateClasses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:classes:delete')")
    public CommonResult<Boolean> deleteClasses(@RequestParam("id") Long id) {
        classesService.deleteClasses(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级信息")
    @PreAuthorize("@ss.hasPermission('picturebook:classes:delete')")
    public CommonResult<Boolean> deleteClassesList(@RequestParam("ids") List<Long> ids) {
        classesService.deleteClassesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:classes:query')")
    public CommonResult<ClassesRespVO> getClasses(@RequestParam("id") Long id) {
        ClassesRespVO classes = classesService.getClasses(id);
        //根据当前返回的teacherId去查询teacherName
        return success(classes);
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级信息分页")
    @PreAuthorize("@ss.hasPermission('picturebook:classes:query')")
    public CommonResult<PageResult<ClassesDO>> getClassesPage(@Valid ClassesPageReqVO pageReqVO) {
        return success(classesService.getClassesPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级信息 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:classes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassesExcel(@Valid ClassesPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassesDO> list = classesService.getClassesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级信息.xls", "数据", ClassesRespVO.class,
                BeanUtils.toBean(list, ClassesRespVO.class));
    }

    //获取用户所有的班级信息
    @GetMapping("/get-classes")
    @Operation(summary = "获取用户所有的班级信息")
    public CommonResult<List<ClassesAllRespVO>> getClasses(@RequestHeader("token") Integer userId) {
        List<ClassesAllRespVO> list = classesService.getAllClass(userId);
        return success(list);
    }
}